﻿@using Orchard.Utility.Extensions;
@using Orchard.ContentManagement;
@using System.Linq;

@{
    Style.Include("Contrib.Stars.css");
    Script.Require("jQuery");
}
<h4 class="reviews-heading" id="reviews-heading">Reviews</h4>
<div class="reviews-content">
    @if (!Model.ContentPart.UserHasReviewed) {
        string reviewLinkText = "Write a review!";
        string noReviewsYetText = "";
        bool reviewsExist = ((IEnumerable<Contrib.Reviews.Models.Review>)Model.ContentPart.Reviews).Any();
        if (!reviewsExist) {
            noReviewsYetText = "No reviews yet. ";
            reviewLinkText = "Be the first to write a review!";
        }

        string showReviewUri = "#";
        if (!Request.IsAuthenticated)
        {
            showReviewUri = Url.Action("LogOn", "Account", new { area = "Orchard.Users", ReturnUrl = Context.Request.RawUrl });
        }
        <header>
        @if (ContentExtensions.IsPublished(Model.ContentPart.ContentItem))
        {

            <div id="createReviewLinkDiv"><span id="createReviewLinkSpan">@noReviewsYetText<a id="showReviewFormLink" href="@showReviewUri">@reviewLinkText</a></span></div>
            <div id="reviewFormDiv" style="display: none">
                <span class="my-rating">My Rating:</span> @Display.Parts_Stars_NoAverage(ContentPart: Model.ContentPart)
                <span class="my-review">My Review:</span>
                    @using (Html.BeginFormAntiForgeryPost(Url.Action("Create", "Review", new { area = "Contrib.Reviews" }), FormMethod.Post))
                {
                    @Html.TextArea("comment", (TempData["Comment"] ?? "").ToString(), new { maxLength = 1200, rows = 4 })
                    @Html.Hidden("contentId", (int)Model.ContentPart.ContentItem.Id)
                    @Html.Hidden("ReturnUrl", Context.Request.ToUrlString())
                    <p class="review-character-limit"><span id="commentCharactersLeft"></span> character(s) remaining</p>
                    <input id="createReviewButton" type="submit" value="Submit Review" />
                    <a id="cancelCreateReviewLink" href="#">Cancel</a>
                }
            </div>
        } else if (!reviewsExist) {
            <div id="createReviewLinkDiv"><span id="createReviewLinkSpan">@T("There are no reviews for this item.")</span></div>
        }
        </header>
    }
    <ul id="reviewsList">
        @foreach (var review in Model.ContentPart.Reviews)
        {
            double userRating = review.Rating.UserRating;
            var userResultClass = string.Format("stars-value-{0}", userRating);
            string liClass = review.IsCurrentUsersReview ? "mine" : "";
            <li class="@liClass">
                <div class="stars-rating">
                    <div class="stars-current-result @userResultClass">
                        <span class="star-1 a-star">@(review.Rating.UserRating > 0 ? T("*").ToString() : T("-").ToString())</span>
                        <span class="star-2 a-star">@(review.Rating.UserRating > 1 ? T("*").ToString() : T("-").ToString())</span>
                        <span class="star-3 a-star">@(review.Rating.UserRating > 2 ? T("*").ToString() : T("-").ToString())</span>
                        <span class="star-4 a-star">@(review.Rating.UserRating > 3 ? T("*").ToString() : T("-").ToString())</span>
                        <span class="star-5 a-star">@(review.Rating.UserRating > 4 ? T("*").ToString() : T("-").ToString())</span>
                    </div>
                </div>
                <div>
                    <span class="review-username">@review.UserName</span><span class="review-timestamp">@review.CreatedUtc</span>
                    <p class="review-comment">@review.Comment
                    @if (review.IsCurrentUsersReview) {
                        <a href="#" id="deleteReviewLink" data-review-id="@review.Id">Delete</a>
                    }
                    </p>
                </div>
            </li>
        }
    </ul>
    <div><a id="showAllReviewsLink" href="#" style="display: none">Show All</a></div>
</div>
<div id="deleteReviewConfirmationDialogDiv" title="Confirm Deletion" style="display:none">
    <p>@T("Are you sure you wish to delete your review? Your Rating will also be deleted.")</p>
    @using (Html.BeginFormAntiForgeryPost(Url.Action("Delete", "Review", new { area = "Contrib.Reviews" }), FormMethod.Post, new { id = "deleteReviewForm" }))
    {
        <input type="hidden" id="reviewId" name="reviewId" />
        <input type="hidden" id="returnUrl" name="returnUrl" value="@Context.Request.ToUrlString()" />
        <input type="submit" value="Yes" />
        <input type="button" value="No" id="cancelDeleteReviewButton" />
    }
</div>
@using (Script.Foot())
{
    <script type="text/javascript">
    //<![CDATA[
    (function () {
        var numberOfReviewsToShowByDefault = 5;
        var $showAllReviewsLink = $('#showAllReviewsLink');
        var $deleteReviewConfirmationDialogDiv = $('#deleteReviewConfirmationDialogDiv');

        $(function () {
            $deleteReviewConfirmationDialogDiv.dialog({ autoOpen: false, modal: true, resizable: false });
            $('#deleteReviewLink').click(function () {
                $('#reviewId').val($(this).attr("data-review-id"));
                ShowDeleteReviewDialog();
                return false;
            });
            @if (Request.IsAuthenticated)
            {
            <text>$('#showReviewFormLink').click(function () {
                $('#createReviewLinkDiv').slideToggle('fast', function () { $('#reviewFormDiv').slideToggle('fast'); });
                return false;
            });
            $('#cancelCreateReviewLink').click(function () {
                $('#reviewFormDiv').slideToggle('fast', function() { $('#createReviewLinkDiv').slideToggle('fast'); });
                return false;
            });</text>
            }
            $('#deleteReviewForm').submit(function () {
                $('input[type=submit]', this).attr('disabled', 'disabled');
            });
            $('#cancelDeleteReviewButton').click(function () {
                CloseConfirmationDialogDiv();
                return false;
            });
            var rowCount = $('#reviewsList li').length;
            if (rowCount > numberOfReviewsToShowByDefault) {
                SetupToggle();
            }

            if (document.location.hash === '#Reviews') {
                var topPx = $('#reviews-heading').position().top;
                $('body,html').animate({ scrollTop: topPx }, 'slow');
            }

            if ($("#comment").length) {
                var characterCountUpdater = new CharacterCountUpdater($("#comment"), $("#commentCharactersLeft"));
                setInterval(function() { characterCountUpdater.UpdateCharacterCount(); }, 100);
                $("#comment").keypress(function() { characterCountUpdater.UpdateCharacterCount(); });

                if ($("#comment").val().length) {
                    $("#showReviewFormLink").trigger("click");
                }
            }
        });

        function CharacterCountUpdater(commentBox, charactersLeftBox)
        {
            this.commentBox = commentBox;
            this.charactersLeftBox = charactersLeftBox;
            this.maxLength = commentBox.attr("maxlength");
            commentBox.removeAttr("maxlength");
            return this;
        }

        CharacterCountUpdater.prototype.UpdateCharacterCount = function() {
            var remainingLength = this.maxLength - this.commentBox.val().length;
            this.charactersLeftBox.text(remainingLength);
            if (remainingLength < 0) {
                this.charactersLeftBox.addClass("negative");
                this.charactersLeftBox.css('color', 'red');
            } else {
                this.charactersLeftBox.removeClass("negative");
                this.charactersLeftBox.css('color', 'black');
            }
        }

        function ShowDeleteReviewDialog() {
            $deleteReviewConfirmationDialogDiv.dialog('open');
            return false;
        }

        function CloseConfirmationDialogDiv() {
            $deleteReviewConfirmationDialogDiv.dialog('close');
            return false;
        }

        function SetupToggle() {
            var rowsToHide = $('#reviewsList li').slice(numberOfReviewsToShowByDefault);
            rowsToHide.addClass('reviewHidden').hide();
            $showAllReviewsLink.css('display', 'block');
            $showAllReviewsLink.click(ToggleExtraReviewRows);
        }

        function ToggleExtraReviewRows() {
            $newText = 'Show All';
            $showAllReviewsLink.toggleClass('less');
            if ($showAllReviewsLink.text() === $newText) {
                $newText = 'Show Less';
            }
            $showAllReviewsLink.text($newText);
            $('#reviewsList li.reviewHidden').toggle();
            return false;
        }
    })();
    //]]>
    </script>
}